#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <dir.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#include "utils.h"
#include "defs.h"
#include "help.h"
#include "file.h"

int get_file(char *file, char *config)
{
   static char item[4][35]={"(L)oad G-Code File", "Create Or (E)dit G-Code File", "Load (M)achine CFG File","e(X)it"}, **array; // ,**myarray;

   static char myarray [20][38]={" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "};

   char tempmode[20], editcommand[20], ecommand[20];
   static BYTE screen[2000];
   int curnt=0, curnt2=0, page=0, i, tot, done, msg=NULL, ch;
   struct find_t ffblk;

   gettext(21,1,60,25,screen);
   while(1)
     {window(21,1,60,25);
      textbackground(CYAN);
      clrscr();
      box(21,3,60,10, CYAN,BLACK,WHITE,BLACK);
      textcolor(BLUE);
      window(21,1,60,25);
      gotoxy(15,25); cprintf("<Esc> To Exit");
      gotoxy(6,2); cprintf(" GCode File:");
      gotoxy(6,3); cprintf("Config File:");
		textcolor(BLACK);
      gotoxy(19,2); cprintf("%s",file);
      gotoxy(19,3); cprintf("%s",config);

		textcolor(BLACK);
      for(i=0;i<4;i++) {gotoxy(4,i+5); cprintf("%s",item[i]);}
      while (1)
        {gotoxy(4,curnt+5); textbackground(BLUE);textcolor(WHITE);
         cprintf("%s",item[curnt]); ch=getch();
         gotoxy(4,curnt+5); textbackground(CYAN);textcolor(BLACK);
         cprintf("%s",item[curnt]);
         if (ch == 13 || ch == 32 || ch == 'L' || ch == 'l' || ch == 'E' || ch == 'e' || ch == 'M' || ch == 'm' || ch == 'X' || ch == 'x')
           {
            if (ch == 'L' || ch== 'l') {curnt=0;}
            if (ch == 'E' || ch== 'e') {curnt=1;}
            if (ch == 'M' || ch== 'm') {curnt=2;}
            if (ch == 'X' || ch== 'x') {curnt=3;}

            if(curnt==0) {if(chdir("GCODE")!=0) continue; else sprintf(tempmode,"GCODE");}
            if(curnt==1) {if(chdir("GCODE")!=0) continue; else sprintf(tempmode,"EDIT");}
            if(curnt==2) {if(chdir("CONFIG")!=0) continue; else sprintf(tempmode,"CONFIG");}
            if(curnt==3) {puttext(21,1,60,25,screen); return MAYBEQUIT;}

            gotoxy(4,curnt+5); textbackground(GREEN);
            cprintf("%s",item[curnt]); textbackground(CYAN);
            break;
           }
         if (ch == 27) {puttext(21,1,60,25,screen); return msg;}
         if (ch > 0) continue;
         ch = getch();
         switch (ch)
           {case 72 : --curnt; break;  //up arrow
            case 80 : ++curnt; break;  //down arrow
            case 59 :
               if(curnt==0) help(41);
               else if(curnt==2) help(42);
               else help(50);
               break;
           }
         if (curnt < 0) curnt=0;
         if (curnt > 3) curnt=3;
        }

      tot=0;
      if(strcmp(tempmode,"GCODE")==0) done=_dos_findfirst("*.TXT",_A_NORMAL,&ffblk);
      if(strcmp(tempmode,"EDIT")==0) done=1;
      if(strcmp(tempmode,"CONFIG")==0) done=_dos_findfirst("*.CFG",_A_NORMAL,&ffblk);
      while (!done) {tot++; done=_dos_findnext(&ffblk);}

        array=(char**) malloc(tot*sizeof(char *));
      for(i=0;i<tot;i++) array[i]=(char*) malloc(20);

      //store file names in array
      i=0;
      if(strcmp(tempmode,"GCODE")==0) done=_dos_findfirst("*.TXT",_A_NORMAL,&ffblk);
      if(strcmp(tempmode,"CONFIG")==0) done=_dos_findfirst("*.CFG",_A_NORMAL,&ffblk);
      while (!done) {sprintf(array[i],"%s",ffblk.name); done=_dos_findnext(&ffblk); i++;}


      //run the editor if that is what is selected
      if(strcmp(tempmode,"EDIT")==0)
        {if(strcmp(file,"NONE")==0)
           {if(spawnlp(P_WAIT,"edit","edit",NULL)==-1 && spawnlp(P_WAIT,"e","e",NULL)==-1) {chdir(".."); help(50); chdir("GCODE");}
           }
         else
           {if(spawnlp(P_WAIT,"edit","edit",file,NULL)==-1 && spawnlp(P_WAIT,"e","e",file,NULL)==-1) {chdir(".."); help(50); chdir("GCODE");}
           }
         _setcursortype(_NOCURSOR);
        }

      //show files in lower box

      if(tot!=0)
       // sort the filenames in the array using Alan's custom sort routine
        asmsort (array,i) ;
      {box(21,10,60,25, CYAN,BLACK,WHITE,BLACK); window(21,1,60,25);}
      textcolor(BLUE); gotoxy(15,25); cprintf("<Esc> To Exit"); textcolor(BLACK);

      while (tot!=0)
        {if(curnt2==23) {
        						  page++; box(21,10,60,25, CYAN,BLACK,WHITE,BLACK); window(21,1,60,25);
                          textcolor(DARKGRAY); gotoxy(15,25); cprintf("<Esc> To Exit"); textcolor(BLACK);
       						}
         if(curnt2==0 && page>0) {
         								 page--; box(21,10,60,25, CYAN,BLACK,WHITE,BLACK); window(21,1,60,25);
                                  textcolor(DARKGRAY); gotoxy(15,25); cprintf("<Esc> To Exit"); textcolor(BLACK);
                                 }
         if(page==0)
           {for(i=0;i<min(tot,23);i++)
              {if(i<12) gotoxy(4,i+12); else gotoxy(22,i); textcolor(BLACK); cprintf("%s",array[i]);}
            if(tot>(page+1)*22+1) {gotoxy(22,i); cprintf("           %c",26);}
           }
         else
           {gotoxy(4,12); textcolor(BLACK); cprintf("%c           ",27);
            for(i=0;i<min(tot-(page*22+1),22);i++)
              {if(i<11)gotoxy(4,i+13); else gotoxy(22,i+1); textcolor(BLACK); cprintf("%s",array[i+page*22+1]);}
            if(tot>(page+1)*22+1) {gotoxy(22,i+1); cprintf("           %c",26);}
           }
         curnt2=0;
         while (1)

           {if(curnt2<12) gotoxy(4,curnt2+12); else gotoxy(22, curnt2); textbackground(BLUE);textcolor(WHITE);
            if(curnt2==23) cprintf("           %c",26);
            else if (curnt2==0 && page!=0)
            	{
               cprintf("%c           ",27);
               }
            else cprintf("%s",array[curnt2+page*22]);
            ch=getch();
            if(curnt2<12)gotoxy(4,curnt2+12); else gotoxy(22, curnt2); textbackground(CYAN);textcolor(BLACK);
            if(curnt2==23) cprintf("           %c",26);
            else if(curnt2==0 && page!=0)
            	{
               cprintf("%c           ",27);
               }
            else cprintf("%s",array[curnt2+page*22]);
            if (ch == 13 || ch == 32)
              {if(curnt2==23 || (page!=0 && curnt2==0)) break;
               box(21,1,60,6, BLUE,BLACK,WHITE,WHITE);
               textcolor(LIGHTGRAY);
               gotoxy(4,3); cprintf("     Load File:");
               gotoxy(4,4); cprintf("<Enter> To Accept  <Esc> To Abort");
               textcolor(WHITE);
               gotoxy(20,3); cprintf("%s",array[curnt2+page*22]);
               gotoxy(5,4); cprintf("Enter");
               gotoxy(24,4); cprintf("Esc");

               // my new file display box

               if (strcmp(tempmode,"GCODE")==0)
						{
                   box(21,6,60,25, BLUE,BLACK,WHITE,WHITE);
               	 asmshow (myarray, array,(curnt2+page*22));
                   for(i=0;i<16;i++){gotoxy(4,(i+3));cprintf("%s",myarray[i]);};
       				}

               while(1)
                 {ch=getch();
                  if(ch==27 || ch==13 || ch==32) break;}
               if(ch==27) break;
               if(strcmp(tempmode,"CONFIG")==0) {sprintf(config,"%s",array[curnt2+page*22]); msg=1; break;}
               else {
               		sprintf(file,"%s",array[curnt2+page*22]);
					      for(i=0;i<tot;i++) free(array[i]); free(array);
					      curnt2=0; page=0;
					      chdir("..");
                     puttext(21,1,60,25,screen);
                		return msg;
               		}
			         }

            if (ch == 27) break;

            if (ch > 0) continue;
            ch = getch();
            switch (ch)
              {
               case 72 : if(curnt2>0)--curnt2; break;  // up arrow
               case 80 : ++curnt2; break;  //down arrow
               case 77 : if(curnt2+12<=min(tot-(22*page)-1,23)) curnt2=curnt2+12; break;  //right arrow
               case 75 : if(curnt2-12>=0) curnt2=curnt2-12; break;  //left arrow
               case 59 : help(45); break;

               case 81 : if ((tot>(page+1)*22+1))
               			{ curnt2=0;  page++;		// pg dn
                           {
                             box(21,10,60,25, CYAN,BLACK,WHITE,BLACK); window(21,1,60,25);
                              textcolor(DARKGRAY); gotoxy(15,25); cprintf("<Esc> To Exit"); textcolor(BLACK);
                           }
          		           if(page==0)
						           {for(i=0;i<min(tot,23);i++)
              							{if(i<12) gotoxy(4,i+12); else gotoxy(22,i); cprintf("%s",array[i]);}
							             if(tot>(page+1)*22+1) {gotoxy(22,i);  cprintf("           %c",26);}
           						  }
							         else
							            {gotoxy(4,12); cprintf("           %c",26);
						                for(i=0;i<min(tot-(page*22+1),22);i++)
						               {if(i<11)gotoxy(4,i+13); else gotoxy(22,i+1); cprintf("%s",array[i+page*22+1]);}
								            if(tot>(page+1)*22+1) { gotoxy(22,i+1); cprintf("           %c",26);}
                                 }
               				break; }
                           break;

               case 73 : {curnt2=0; if (page == 0) break; // pg up
               			  page--;
							     {
                           box(21,10,60,25, CYAN,BLACK,WHITE,BLACK); window(21,1,60,25);
                           textcolor(DARKGRAY); gotoxy(15,25); cprintf("<Esc> To Exit"); textcolor(BLACK);
                          }
          		           if(page==0)
						           {for(i=0;i<min(tot,23);i++)
              							{if(i<12) gotoxy(4,i+12); else gotoxy(22,i); cprintf("%s",array[i]);}
							             if(tot>(page+1)*22+1) { gotoxy(22,i);  cprintf("           %c",26);}
           								}
							         else
							            {gotoxy(4,12);  cprintf("           %c",26);
						                for(i=0;i<min(tot-(page*22+1),22);i++)
						               {if(i<11)gotoxy(4,i+13); else gotoxy(22,i+1); cprintf("%s",array[i+page*22+1]);}
								            if(tot>(page+1)*22+1) { gotoxy(22,i+1);  cprintf("           %c",26);}
							            }
               				break; }
               }
            if (curnt2 < 0) curnt2=min(tot-(22*page)-1,23);
            if (curnt2 > min(tot-(22*page)-1,23)) curnt2=(min(tot-(22*page)-1,23));
           }
         if(ch==27) break;

         if(curnt2==23 || (page!=0 && curnt2==0)) continue;
         break;
        }
      for(i=0;i<tot;i++) free(array[i]); free(array);
      curnt2=0; page=0;
      chdir("..");
     }
}


